package com.example.meituan.dao;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.meituan.activity.bean.OrderBean;
import com.example.meituan.db.DBUntil;

import java.util.ArrayList;
import java.util.List;

public class OrderDao {
    public static SQLiteDatabase db= DBUntil.con;

    // 插入订单
    public long insertOrder(OrderBean order) {
        ContentValues values = new ContentValues();
        values.put("s_id", order.getsId());
        values.put("s_order_time", order.getsOrderTime());
        values.put("s_business_id", order.getsBusinessId());
        values.put("s_user_id", order.getsUserId());
        values.put("s_order_sta", order.getsOrderSta());
        values.put("s_order_address", order.getsOrderAddress());
        values.put("s_order_detials_id", order.getsOrderDetialsId());

        return db.insert("d_orders", null, values);
    }

    // 更新订单
    public static int updateOrder(OrderBean order) {
        ContentValues values = new ContentValues();
        values.put("s_order_time", order.getsOrderTime());
        values.put("s_business_id", order.getsBusinessId());
        values.put("s_user_id", order.getsUserId());
        values.put("s_order_sta", order.getsOrderSta());
        values.put("s_order_address", order.getsOrderAddress());
        values.put("s_order_detials_id", order.getsOrderDetialsId());

        return db.update("d_orders", values, "s_id=?", new String[]{order.getsId()});
    }

    // 删除订单
    public int deleteOrder(String orderId) {
        return db.delete("d_orders", "s_id=?", new String[]{orderId});
    }
    public static List<OrderBean> getAllOrdersByBusinessId(String businessId) {
        List<OrderBean> list = new ArrayList<>();
        String query = "SELECT * FROM d_orders WHERE s_business_id = ? AND s_order_sta != ?";
        Cursor cursor = db.rawQuery(query, new String[]{businessId, "已完成"});

        while (cursor.moveToNext()) {
            @SuppressLint("Range") String id = cursor.getString(cursor.getColumnIndex("s_id"));
            @SuppressLint("Range") String orderTime = cursor.getString(cursor.getColumnIndex("s_order_time"));
            @SuppressLint("Range") String userId = cursor.getString(cursor.getColumnIndex("s_user_id"));
            @SuppressLint("Range") String sta = cursor.getString(cursor.getColumnIndex("s_order_sta"));
            @SuppressLint("Range") String address = cursor.getString(cursor.getColumnIndex("s_order_address"));
            @SuppressLint("Range") String detials_id = cursor.getString(cursor.getColumnIndex("s_order_detials_id"));

            OrderBean orderBean = new OrderBean();
            orderBean.setsBusinessId(businessId);
            orderBean.setsId(id);
            orderBean.setsOrderAddress(address);
            orderBean.setsOrderSta(sta);
            orderBean.setsOrderTime(orderTime);
            orderBean.setsUserId(userId);
            orderBean.setsOrderDetialsId(detials_id);

            list.add(orderBean);
        }

        cursor.close();
        return list;
    }

    public static List<OrderBean> getAllOrdersByUserId(String userId) {
        List<OrderBean> list = new ArrayList<>();
        String query = "SELECT * FROM d_orders WHERE s_business_id = ? AND s_order_sta != ?";
        Cursor cursor = db.rawQuery(query, new String[]{userId, "已完成"});

        while (cursor.moveToNext()) {
            @SuppressLint("Range") String id = cursor.getString(cursor.getColumnIndex("s_id"));
            @SuppressLint("Range") String orderTime = cursor.getString(cursor.getColumnIndex("s_order_time"));
            @SuppressLint("Range") String businessId = cursor.getString(cursor.getColumnIndex("s_business_id"));
            @SuppressLint("Range") String sta = cursor.getString(cursor.getColumnIndex("s_order_sta"));
            @SuppressLint("Range") String address = cursor.getString(cursor.getColumnIndex("s_order_address"));
            @SuppressLint("Range") String detials_id = cursor.getString(cursor.getColumnIndex("s_order_detials_id"));

            OrderBean orderBean = new OrderBean();
            orderBean.setsBusinessId(businessId);
            orderBean.setsId(id);
            orderBean.setsOrderAddress(address);
            orderBean.setsOrderSta(sta);
            orderBean.setsOrderTime(orderTime);
            orderBean.setsUserId(userId);
            orderBean.setsOrderDetialsId(detials_id);

            list.add(orderBean);
        }

        cursor.close();
        return list;
    }

    // 获取单个订单
    public OrderBean getOrderById(String orderId) {
        Cursor cursor = db.query("d_orders", null, "s_id=?", new String[]{orderId}, null, null, null);

        OrderBean order = OrderBean.fromCursor(cursor);
        cursor.close();
        return order;
    }

    // 获取所有订单
    public ArrayList<OrderBean> getAllOrders() {
        Cursor cursor = db.query("d_orders", null, null, null, null, null, null);

        ArrayList<OrderBean> orders = OrderBean.fromCursorList(cursor);
        cursor.close();
        return orders;
    }

}
